package io.afu.validator.Implement;

import io.afu.validator.Annimation.ChineseMobile;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author RffanLAB.方露宇
 * @version 0.1
 */
public class ChineseMobileValidatorImpl implements ConstraintValidator<ChineseMobile,String> {

    private Boolean allowEmpty;

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (s == null || "".equals(s)){
            return this.allowEmpty;
        }else {
            String patten = "^1[34578]\\d{9}$";
            Pattern p = Pattern.compile(patten);
            Matcher m = p.matcher(s);
            return  m.find();
        }
    }

    @Override
    public void initialize(ChineseMobile constraintAnnotation) {
        this.allowEmpty = constraintAnnotation.allowEmpty();
    }
}
